zoom = 1;
function initmapeitor() {
    updateTribalList();
    loadTribal();
}

function loadTribal() {
    var url = './rpc/mapinfo.php';
    execAjax(url, "tribalAdded");
}

function addTribal(idtribal) {
    var url = './rpc/mapinfo.php';
    var pars = 'ally=' + idtribal + '&submit=Añadir';
    execAjax(url + "?" + pars, "tribalAdded");
}

function tribalAdded(str) {
    document.getElementById("map").innerHTML = "";
    var tmp = str.split("\|\|\|");
    for (i in tmp) {
        if ( tmp[i] ) {
            tmp2 = tmp[i].split("\|\*\|");
            x     = tmp2[0] * zoom;
            y     = tmp2[1] * zoom;
            name  = tmp2[2];
            color = tmp2[3];
            //debug("i " + i + ":" + tmp[i] + "<br>");
            //debug(x + ">" + y + ">" + name + ">" + color);
            createMapPoint(x, y, name, color ,i);
        }
    }
    updateTribalList();
}

function debug(str) {
    document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + str + "\n" ;    
}

function updateTribalList() {
    var url = './rpc/triballist.php';
    execAjax(url, "tribalListUpdated");
}

function tribalListUpdated(str) {
    document.getElementById("triballist").innerHTML = str;
}

function removeTribal(idtribal) {
    var url = './rpc/mapinfo.php';
    var pars = 'remove=' + idtribal;
    execAjax(url + "?" + pars, "tribalAdded");

}

function mapsize(z) {
    zoom = z;
    loadTribal();
}

function getAllySelect(q) {
    var url = './rpc/allyselectlist.php';
    var pars = 'q=' + q;
    execAjax(url + "?" + pars, "allySelect");
}

function allySelect(str) {
    document.getElementById("ally").options.length = 0;
    var ally = str.split("\|\*\|");
	for (i=0;i<(ally.length-1);i++){
		values = ally[i].split("\|\+\|");
		document.getElementById("ally").options[document.getElementById("ally").length] = new Option(values[1], values[0]);
	}

}

function createMapPoint(x, y, name, color, id) {
    dv = document.createElement('div'); // create dynamically div tag
    dv.setAttribute('id','dv' + id);       //give id to it
    dv.style.position="absolute";
    y = parseInt(y) + parseInt(document.getElementById('map').offsetTop);
    dv.style.left= x + 'px';
    dv.style.top=  y + 'px';
    dv.style.width='300px';

    point = document.createElement('div');
    point.setAttribute('id','point' + id);       //give id to it
    point.style.width='4px';
    point.style.height='4px';
    point.style.backgroundColor=color;
    /*
    point.onmouseover=function(){
        justShowMe('dv' + id, color);
    } ;
    point.onmouseout=function(){
        justHideMe('dv' + id);
    } ;
    */

    
    text = document.createElement('div');
    point.setAttribute('id','text' + id);       //give id to it
    text.style.fontSize='xx-small';
    text.innerHTML=name;
    
    dv.appendChild(point);
    dv.appendChild(text);
    document.getElementById('map').appendChild(dv);
}

/*
function justHideMe (dv) {
    document.getElementById(dv).style.border="0";
}
function justShowMe(dv , c) {
    //alert("HOLA");
    debug("HOLA " + "1px solid " + c + "<br>");
    document.getElementById(dv).style.border="1px solid " + c;
}
*/
